/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.csv.bean.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import com.genia.toolbox.projects.csv.bean.FolderDescriptor;
import com.genia.toolbox.projects.csv.bean.MutableFolderDescriptor;
import com.genia.toolbox.projects.csv.bean.MutableMappingDescriptor;

/**
 * implementation of {@link MutableMappingDescriptor}.
 */
public class MappingDescriptorImpl
    implements MutableMappingDescriptor
{

  /**
   * the default date format.
   */
  public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");

  /**
   * the default datetime format.
   */
  public static final SimpleDateFormat DEFAULT_DATE_TIME_FORMAT = new SimpleDateFormat("dd/MM/yyyy - HH:mm:ss");

  /**
   * the default time format.
   */
  public static final SimpleDateFormat DEFAULT_TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");

  /**
   * the format of dates in the csv file.
   */
  private SimpleDateFormat dateFormat = DEFAULT_DATE_FORMAT;

  /**
   * the format of datetimes in the csv file.
   */
  private SimpleDateFormat datetimeFormat = DEFAULT_DATE_TIME_FORMAT;

  /**
   * the locale that corresponds to the main language in csv import mapping.
   */
  private Locale mainLocale = Locale.ENGLISH;

  /**
   * list of supported locales that corresponds to the supported languages in
   * csv import mapping.
   */
  private List<Locale> supportedLocales = new ArrayList<Locale>();

  /**
   * the format of times in the csv file.
   */
  private SimpleDateFormat timeFormat = DEFAULT_TIME_FORMAT;

  /**
   * the {@link List} of {@link FolderDescriptor}.
   */
  private List<FolderDescriptor> folders = new ArrayList<FolderDescriptor>();

  /**
   * the folders indexed by their names.
   */
  private transient final Map<String, FolderDescriptor> foldersByName = new HashMap<String, FolderDescriptor>();



  /**
   * add a {@link Locale} to the supported {@link Locale}s.
   * 
   * @param locale
   *          the {@link Locale} to add
   */
  public void addSupportedLocale(Locale locale)
  {
    getSupportedLocales().add(locale);
  }



  /**
   * getter for the mainLocale property.
   * 
   * @return the mainLocale
   */
  public Locale getMainLocale()
  {
    return mainLocale;
  }



  /**
   * getter for the supportedLocales property.
   * 
   * @return the supportedLocales
   */
  public List<Locale> getSupportedLocales()
  {
    return supportedLocales;
  }



  /**
   * setter for the mainLocale property.
   * 
   * @param mainLocale
   *          the mainLocale to set
   */
  public void setMainLocale(Locale mainLocale)
  {
    this.mainLocale = mainLocale;
  }



  /**
   * setter for the supportedLocales property.
   * 
   * @param supportedLocales
   *          the supportedLocales to set
   */
  public void setSupportedLocales(List<Locale> supportedLocales)
  {
    this.supportedLocales = supportedLocales;
  }



  /**
   * add a new {@link FolderDescriptor} to the list.
   * 
   * @param folder
   *          the {@link FolderDescriptor} to add
   */
  public void addFolder(MutableFolderDescriptor folder)
  {
    this.folders.add(folder);
    this.foldersByName.put(folder.getFolderName(), folder);
    folder.setMappingDescriptor(this);
  }



  /**
   * getter for the folders property.
   * 
   * @return the folders
   */
  public List<FolderDescriptor> getFolders()
  {
    return folders;
  }



  /**
   * setter for the folders property.
   * 
   * @param folders
   *          the folders to set
   */
  public void setFolders(List<FolderDescriptor> folders)
  {
    this.folders = folders;
  }



  /**
   * returns the {@link FolderDescriptor} associated to the given name.
   * 
   * @param name
   *          the name of the {@link FolderDescriptor} to retrieve
   * @return the {@link FolderDescriptor} associated to the given name
   * @see com.genia.toolbox.projects.csv.bean.MappingDescriptor#getFolder(java.lang.String)
   */
  public FolderDescriptor getFolder(String name)
  {
    return foldersByName.get(name);
  }



  /**
   * getter for the dateFormat property.
   * 
   * @return the dateFormat
   */
  public SimpleDateFormat getDateFormat()
  {
    return dateFormat;
  }



  /**
   * getter for the datetimeFormat property.
   * 
   * @return the datetimeFormat
   */
  public SimpleDateFormat getDatetimeFormat()
  {
    return datetimeFormat;
  }



  /**
   * getter for the timeFormat property.
   * 
   * @return the timeFormat
   */
  public SimpleDateFormat getTimeFormat()
  {
    return timeFormat;
  }



  /**
   * setter for the dateFormat property.
   * 
   * @param dateFormat
   *          the dateFormat to set
   */
  public void setDateFormat(SimpleDateFormat dateFormat)
  {
    this.dateFormat = dateFormat;
  }



  /**
   * setter for the datetimeFormat property.
   * 
   * @param datetimeFormat
   *          the datetimeFormat to set
   */
  public void setDatetimeFormat(SimpleDateFormat datetimeFormat)
  {
    this.datetimeFormat = datetimeFormat;
  }



  /**
   * setter for the timeFormat property.
   * 
   * @param timeFormat
   *          the timeFormat to set
   */
  public void setTimeFormat(SimpleDateFormat timeFormat)
  {
    this.timeFormat = timeFormat;
  }

}
